package com.example.service;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.AirLane;

import com.example.entity.BaseAirLane;
import com.example.entity.BaseTakeoffLand;
import com.example.mapper.BaseAirLaneMapper;
import com.example.mapper.BaseTakeoffLandMapper;
import com.example.service.interfaces.BaseAirLaneInterface;
import com.example.service.interfaces.BaseTakeoffLandInterface;
import com.example.trans.AirLaneWp;
import com.example.trans.Wp;
import com.example.trans.transBaseAirLane;
import com.example.trans.transBaseTakeoffLand;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @program: microservice
 * @description:
 * @author: Aprilies
 * @create: 2024-09-10 16:51
 **/

@Service
public class BaseTakeoffLandService extends ServiceImpl<BaseTakeoffLandMapper, BaseTakeoffLand> implements BaseTakeoffLandInterface {

    @Autowired
    private BaseTakeoffLandMapper baseTakeoffLandMapper ;

    public List<transBaseTakeoffLand> findAll(){
        List<BaseTakeoffLand> baseTakeoffLands = baseTakeoffLandMapper.selectList(null);
        System.out.println(baseTakeoffLands);
        return baseTakeoffLands.stream().map(baseTakeoffLand -> {
            transBaseTakeoffLand transBaseTakeoffLand = new transBaseTakeoffLand();
            BeanUtils.copyProperties(baseTakeoffLand, transBaseTakeoffLand);
            String wp = baseTakeoffLand.getWaypointsCoordinates();
            System.out.println(wp);
            if(wp != null && !wp.isEmpty()) {
                JSONArray jsonArray = JSONArray.parseArray(wp);
                List<Wp> takeoffWp = jsonArray.toJavaList(Wp.class);
                System.out.println(takeoffWp);
                transBaseTakeoffLand.setWaypointsCoordinates(takeoffWp);
            }
            return transBaseTakeoffLand;
        }).collect(Collectors.toList());
    }

    public Optional<transBaseTakeoffLand> findByID(Integer id) {
        BaseTakeoffLand baseTakeoffLand = baseTakeoffLandMapper.selectById(id);
        if (baseTakeoffLand == null) {
            return Optional.empty();
        }
        transBaseTakeoffLand transBaseTakeoffLand = new transBaseTakeoffLand();
        BeanUtils.copyProperties(baseTakeoffLand, transBaseTakeoffLand);
        // 单独处理 waypointsCoordinates 字段
        String wp = baseTakeoffLand.getWaypointsCoordinates();
        if (wp != null && !wp.isEmpty()) {
            JSONArray jsonArray = JSONArray.parseArray(wp);
            List<Wp> takeoffWp = jsonArray.toJavaList(Wp.class);
            transBaseTakeoffLand.setWaypointsCoordinates(takeoffWp);
        }
        return Optional.of(transBaseTakeoffLand);
    }

    public transBaseTakeoffLand isave(transBaseTakeoffLand transTakeoffLand) {
        BaseTakeoffLand baseTakeoffLand = new BaseTakeoffLand();
        // 使用 BeanUtils 复制属性
        BeanUtils.copyProperties(transTakeoffLand, baseTakeoffLand);

        // 将 List<AirLaneWp> 转换为 JSON 字符串并设置到 waypointsCoordinates
        List<Wp> waypoints = transTakeoffLand.getWaypointsCoordinates();
        if (waypoints != null && !waypoints.isEmpty()) {
            String wpJson = JSONArray.toJSONString(waypoints);
            baseTakeoffLand.setWaypointsCoordinates(wpJson);
        }
        // 插入数据
        baseTakeoffLandMapper.insert(baseTakeoffLand);
        transTakeoffLand.setId(baseTakeoffLand.getId());
        return transTakeoffLand;
    }

    // 只返回更新的字段数据
    public Optional<transBaseTakeoffLand> update(transBaseTakeoffLand user) {
        BaseTakeoffLand baseTakeoffLand = new BaseTakeoffLand();
        BeanUtils.copyProperties(user, baseTakeoffLand);
        List<Wp> waypoints = user.getWaypointsCoordinates();
        if (waypoints != null && !waypoints.isEmpty()) {
            String wpJson = JSONArray.toJSONString(waypoints);
            baseTakeoffLand.setWaypointsCoordinates(wpJson);
        }
        int i = baseTakeoffLandMapper.updateById(baseTakeoffLand);
        if(i == 1){
            return Optional.of(user);
        }
        return Optional.empty();
    }

    public boolean delete(Long id) {
        if (baseTakeoffLandMapper.selectById(id) != null) {
            baseTakeoffLandMapper.deleteById(id);
            return true;
        }
        return false;
    }

}
